#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;

#define NUM 2
int* g_add;

void* func1(void* arg)
{
  char* str = static_cast<char*>(arg);
  cout << str << endl;
  int local_val = 10;
  g_add = &local_val;
  while(1)
  {
   cout << "I am " << pthread_self() <<"my local_variable values: " << local_val << endl; 
   sleep(5);
  }
}
void* func2(void* arg)
{
  char* str = static_cast<char*>(arg);
  cout << str << endl;
  while(1)
  {
    // 未知哪个线程先执行
    if(g_add != nullptr)  
    {
      cout << pthread_self() << "let other thread local_variable add 100" <<endl;
      *g_add += 100;
    }
    sleep(2);
  }
}

int main()
{
  pthread_t pid[NUM]; 
  pthread_create(&pid[0],nullptr,func1,(void*)"thread 1");
  pthread_create(&pid[1],nullptr,func2,(void*)"thread 2");

  pthread_join(pid[0],nullptr);
  pthread_join(pid[1],nullptr);
  //.....
  return 0;
}




/*
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<vector>
using namespace std;

class A
{
public:
	virtual void show()
	{
		cout << "A::show()" << endl;
	}
};

class B :public A
{
public:
	void show()
	{
		cout << "B::show()" << endl;
	}
	virtual void list()
	{
		cout << "B::list()" << endl;
	}
};


class D :public A
{
public:
	void show()
	{
		cout << "D::show()" << endl;
	}
};

void func(A* pa)
{
	B* pb = dynamic_cast<B*>(pa);
	if (pb != nullptr) //转换成功
		pb->list();
	else
		cerr << "转换失败！请传入B类对象的地址" << endl;
}
int main()
{
	A a;
	B b;
	D d;
	func(&a);
	func(&d);
	func(&b);
	return 0;
}
*/